<template>
  <div>
    <!-- 面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            class="input"
            clearable
            v-model="queryInfo.query"
            @clear="clearInput"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchFrom"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <!-- 添加按钮 -->
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160px">
          <!-- 利用插槽将每一行的时间都加上时间的过滤器 -->
          <template v-slot="slotScope">{{slotScope.row.add_time|dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="170px">
          <template v-slot="slotScope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editFrom(slotScope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteFrom(slotScope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.queryInfo"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 编辑表单的对话框 -->
  </div>
</template>

<script>

export default {
  data() {
    return {
      // 获取商品列表时的请求参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5,
      },
      //  商品总条数
      total: 0,
      //  获取到的商品列表对象
      goodsList: [],
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //   获取商品列表
    async getGoodsList() {
      // 发送请求
      const data = await this.$http.get('goods', { params: this.queryInfo });
      if (!data) return this.$message.error('获取商品列表数据失败！');
      this.$message.success('获取商品列表成功！');
      console.log(data);
      //   将数据赋值给列表
      this.goodsList = data.goods;
      this.total = data.total;
    },
    // 每页条数改变时会触发 pageSize
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      // 重新渲染
      this.getGoodsList();
    },
    // 当前页改变时会触发 pagenum
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      // 重新渲染
      this.getGoodsList();
    },
    // 实现输入框的搜索功能
    searchFrom() {
      this.getGoodsList();
    },
    // 清空输入框的事件
    clearInput() {
      // 重新发请求将所有的数据列表再显示出来
      this.getGoodsList();
    },
    // 添加商品按钮
    goAddGoods() {
      // 跳转到添加商品的页面
      this.$router.push('/goods/add')
    },

    // 编辑表单函数
    editFrom(row) {
      // console.log(row.goods_name);
      // 将当前点击的这一行的数据添加到对话框的输入框中
    },
    // 删除表单数据
    async deleteFrom(row) {
      // console.log(row);
      // 得到了当前商品的id 直接发送请求删除
      const data = await this.$http.delete(`goods/${row.goods_id}`);
      console.log(data);
      if (!data) return
      if (data.status !== 200) return this.$message.error(data.msg);
      this.$message.success(data.msg);
      // 刷新数据列表
      this.getGoodsList();
    },
  },
};
</script>

<style lang="less" scoped>
</style>
